package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 分页查询
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<RoleVo> selectPage(RoleDto roleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        Page<RoleVo> page =  roleMapper.selectPage(roleDto.getRoleName());

        return PageResponse.of(page,RoleVo.class);
    }

    /**
     * 新增角色
     * @param roleDto
     */
    @Override
    public void add(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        role.setCreateTime(LocalDateTime.now());
        role.setCreateBy(1L);
        role.setRemark(roleDto.getRoleName());

        roleMapper.insert(role);
    }

    /**
     * 根据RoleId查询中间表
     * @param roleId
     * @return
     */
    @Override
    public List<String> getByRoleId(Long roleId) {
        return  roleResourceMapper.getByRoleId(roleId);
    }

    @Override
    @Transactional
    public void deleteRole(Long roleId) {
        //1.根据id查询角色信息 判断状态 如果是已禁用则进行删除 如果不是则报错
        Role role = roleMapper.getById(roleId);
        if(null != role && role.getDataState().equals(SuperConstant.DATA_STATE_0)){
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }

        //2.删除角色和资源得关系数据
        roleResourceMapper.deleteByRoleId(roleId);

        //3.删除角色数据
        roleMapper.deleteByRoleId(roleId);

    }

    @Override
    @Transactional
    public Boolean update(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
//        Role role = roleDto.clone();
        role.setUpdateTime(LocalDateTime.now());
        role.setUpdateBy(1L);

        //状态的修改 如果修改为禁用 那么需要查询当前角色下是否有用户
        if(null!=role.getDataState() && role.getDataState().equals(SuperConstant.DATA_STATE_1)){
            Integer count = userRoleMapper.countByRoleId(roleDto.getId());
            if(null != count && count > 0){
                throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
            }
        }
        //对role基础表进行插入
        Boolean flag =roleMapper.update(role);

        //删除该角色对应的角色资源中间表的所有该角色数据
        roleResourceMapper.deleteByRoleId(roleDto.getId());
        if(ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())){
            return flag;
        }

        //声明中间表的集合
        List<RoleResource> roleResourceList = Lists.newArrayList();


        String[] checkedResourceNos = roleDto.getCheckedResourceNos();
        List<String> list = Arrays.asList(checkedResourceNos);
        list.stream().map(no->{
            RoleResource roleResource =new RoleResource();

            roleResource.setRoleId(roleDto.getId());
            roleResource.setResourceNo(no);
            roleResource.setCreateTime(LocalDateTime.now());
            roleResource.setUpdateTime(LocalDateTime.now());
            roleResource.setDataState(SuperConstant.DATA_STATE_0);

            roleResourceList.add(roleResource);
            return roleResource;
        }).collect(Collectors.toList());

        roleResourceMapper.insertBatch(roleResourceList);

        return flag;
    }

    @Override
    public List<RoleVo> getAllRoles() {
        return roleMapper.getAllRoles();
    }


}
